//
//  FormDetailViewController.m
//  CWeekly
//
//  Created by  on 12-6-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FormDetailViewController.h"
#import "AppDelegate.h"
#import "DataTools.h"
#import <QuartzCore/QuartzCore.h>
#import "UserCenter.h"
#import "LoginView.h"
#define bottomViewheight 200
#define HEIGHT_S 1024-75-78
#define HEIGHT_H 768-75-78
#define KeyboardHeight_v 262
#define KeyboardHeight 352

@interface FormDetailViewController ()

@end

@implementation FormDetailViewController
static int currentPage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dataArray=[[NSMutableArray alloc]init];
        thumbArray=[[NSMutableArray alloc] init];
        collectionList = [[NSMutableArray alloc] init];

    }
    return self;
}

-(void)initBottomView{
    bottomView=[[UIView alloc] init];
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        bottomView.frame=CGRectMake(0, 748-35,  viewWidth, bottomViewheight);
    }else {
        bottomView.frame=CGRectMake(0, 1004-35,  viewWidth, bottomViewheight);
        NSLog(@"HHHH");
    }
    
    bottomView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.6];
    
    [self.view addSubview:bottomView];
    [self.view bringSubviewToFront:bottomView];
    
    [bottomView release];
    thumbBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    thumbBtn.frame=CGRectMake(10, 0, 35, 35);
    [thumbBtn setImage:[UIImage imageNamed:@"thumb.png"] forState:UIControlStateNormal];
    [thumbBtn addTarget:self action:@selector(bottomAnimate) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:thumbBtn];
    
    contents=[[UIScrollView alloc] init];
    contents.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"contents_bg.png"]];
    [bottomView addSubview:contents];
    [contents release];
    
    contents.frame=CGRectMake(0, 35,  viewWidth, 165);
    contents.delegate=self;
    contents.contentSize=CGSizeMake(dataArray.count*150, 165);
    int tcount=0;
    
    for (int i=0;i<doc.forums.count;i++) {
        Forum*f=[doc.forums objectAtIndex:i];
        for (int j=0;j<f.articles.count;j++ ) {
            Article* a=[f.articles objectAtIndex:j];
            for (int k=0;k<a.pages.count;k++) {
                Page*p=[a.pages objectAtIndex:k];
                //               NSString* directory=[NSString stringWithFormat:@"Documents/0423"];
                NSString*imgPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/%@",mgid,p.t_thumbnails]];
                imgPath=[imgPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                
                //            NSString*imgPath=[NSHomeDirectory() stringByAppendingPathComponent:[directory stringByAppendingPathComponent:p.v_thumbnails]];
                NSData*da=[NSData dataWithContentsOfFile:imgPath];
                UIButton*b=[UIButton buttonWithType:UIButtonTypeCustom];
                b.tag=tcount;
                [b addTarget:self action:@selector(pageSelect:) forControlEvents:UIControlEventTouchUpInside];
                b.frame=CGRectMake(30+(120+30)*tcount, 30, 120, 95);
                [b setBackgroundImage:[UIImage imageWithData:da] forState:UIControlStateNormal];
                [contents addSubview:b];
                [thumbArray addObject:b];
                UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(30+(120+30)*tcount, 140, 120, 20)];
                label.text=a.title;
                label.textColor=[UIColor whiteColor];
                label.backgroundColor=[UIColor clearColor];
                [contents addSubview:label];
                [label release];
                [dataArray addObject:p];
                tcount++;
            }
        }
    }
    
}

-(void)initTopView{
    topview=[[UIView alloc] init];
    topview.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.6];
    topview.frame=CGRectMake(0, 0, viewWidth, 55);
    
    UIButton* home=[UIButton buttonWithType:UIButtonTypeCustom];
    home.frame=CGRectMake(40, 0, 40, 55);
    home.showsTouchWhenHighlighted=YES;
    [home setImage:[UIImage imageNamed:@"home.png"] forState:UIControlStateNormal];
    [home setImage:[UIImage imageNamed:@"home_s.png"] forState:UIControlStateHighlighted];
    [home addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside];
    [topview addSubview:home];
    
    UIButton* listbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    listbtn.frame=CGRectMake(90, 0, 40, 55);
    listbtn.showsTouchWhenHighlighted=YES;
    [listbtn addTarget:self action:@selector(showList) forControlEvents:UIControlEventTouchUpInside];
    [listbtn setImage:[UIImage imageNamed:@"list.png"] forState:UIControlStateNormal];
    [listbtn setImage:[UIImage imageNamed:@"list_s.png"] forState:UIControlStateHighlighted];
    [topview addSubview:listbtn];
    
    UIButton* share=[UIButton buttonWithType:UIButtonTypeCustom];
    [share setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
    share.tag=111;
    [share setImage:[UIImage imageNamed:@"share_s.png"] forState:UIControlStateHighlighted];
    share.showsTouchWhenHighlighted=YES;
    [share addTarget:self action:@selector(showShare) forControlEvents:UIControlEventTouchUpInside];
    [topview addSubview:share];
    
    UIButton*markbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    markbtn.tag=222;
    [markbtn addTarget:self action:@selector(markEvent) forControlEvents:UIControlEventTouchUpInside];
    markbtn.frame=CGRectMake(viewWidth-140, 0, 40, 55);
    [markbtn setImage:[UIImage imageNamed:@"remark.png"] forState:UIControlStateNormal];
    [markbtn setImage:[UIImage imageNamed:@"remark.png_s"] forState:UIControlStateHighlighted];
    [topview addSubview:markbtn];
    
    share.frame=CGRectMake(viewWidth-80, 0, 40, 55);
    
    UIButton*add=[UIButton buttonWithType:UIButtonTypeCustom];
    add.tag=333;
    add.frame=CGRectMake(viewWidth-200, 0, 40, 55);
    
    [add addTarget:self action:@selector(addCollection) forControlEvents:UIControlEventTouchUpInside];
    [add setImage:[UIImage imageNamed:@"addmark.png"] forState:UIControlStateNormal];
    [add setImage:[UIImage imageNamed:@"addmark_s.png"] forState:UIControlStateHighlighted];
    [topview addSubview:markbtn];
    [topview addSubview:add];
    [self.view addSubview:topview];
    [self.view bringSubviewToFront:topview];
    [topview release];
    
}

-(void)initScroll{
    NSLog(@"initScroll");
    if (pageScroll==nil) {
        pageScroll=[[UIScrollView alloc] init];
//        pageScroll.backgroundColor=[UIColor redColor];
    }
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        pageScroll.frame=CGRectMake(0, 0, 1024, 768-20);
    }else {
        pageScroll.frame= CGRectMake(0, 0, 768, 1024-20);
    }
    pageScroll.delegate=self;
    pageScroll.pagingEnabled=YES;
    pageScroll.bounces=NO;
   int loop=dataArray.count;
    pageScroll.contentSize=CGSizeMake(pageScroll.frame.size.width*loop, pageScroll.frame.size.height);
    [self.view addSubview:pageScroll];
    [self.view sendSubviewToBack:pageScroll];
    [pageScroll release];    
    
    UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] init];
    [pageScroll addGestureRecognizer:tap];
    tap.delegate=self;
    [tap release];

}

- (void)addPageToScroll:(UIInterfaceOrientation)interfaceOrientationState{
    NSString*orientationStr;
    if (interfaceOrientationState == UIInterfaceOrientationLandscapeLeft || interfaceOrientationState == UIInterfaceOrientationLandscapeRight) {
        orientationStr=@"H";
    }else {
        orientationStr=@"V";
    }
    for (int i=0; i<dataArray.count; i++) {
        Page*p=[dataArray objectAtIndex:i];
        NSString* imgName=[NSString stringWithFormat:@"%@.jpg", p.num];
        NSString*imgPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/%@/%@",mgid,orientationStr,imgName]];
        imgPath=[imgPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        
        //            NSString*imgPath=[NSHomeDirectory() stringByAppendingPathComponent:[directory stringByAppendingPathComponent:p.v_thumbnails]];
        NSData*da=[NSData dataWithContentsOfFile:imgPath];
        UIImage*im=[UIImage imageWithData:da];
        CGFloat imgH=CGImageGetHeight([im CGImage]);
        CGFloat imgW=CGImageGetWidth([im CGImage]);
        UIImageView* img=[[UIImageView alloc] initWithImage:im];
        img.frame=CGRectMake(i*viewWidth, 0, imgW, imgH);
        img.userInteractionEnabled=YES;
        [pageScroll addSubview:img];
        [img release];
    }
}

-(void)viewWidth:(UIInterfaceOrientation)interfaceOrientation{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        viewWidth=1024.0f;
    }else {
        viewWidth=768.0f;
    }
    
}

-(NSURL*)pathOfIMG:(NSString*)ImgName{
    NSString* path= [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/%@",mgid,ImgName]];
    path=[path stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSURL* url=[NSURL fileURLWithPath:path];
        NSLog(@"html------%@",url);  
        return url;
    }else {
        NSLog(@"html------nil");
        return nil;
    }
}

-(void)ShowBars:(UIGestureRecognizer*)recgongizer{
    [UIView beginAnimations:@"hiden" context:nil ];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3];
    if (topview.alpha) {
        topview.alpha=0;
        list.alpha=0;
        bottomView.alpha=0;
        
        int h;
        if (viewWidth==1024) {
            h=748;
        }else {
            h=1004;
        }
        CGPoint p=bottomView.center;
        if (bottomView.center.y<h) {
            bottomView.center=CGPointMake(p.x, p.y+165);
        }
        
        
    }else {
        topview.alpha=1;
        bottomView.alpha=1;
    }
    [UIView commitAnimations];
    
}

#pragma mark BTN event

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        [self ShowBars:gestureRecognizer];
        return YES;
    }
    return NO;
}
-(void)showShare{
    
    UIViewController* shareViewController=[[UIViewController alloc] init];
    shareViewController.view.frame=CGRectMake(0, 0, 220, 120);
    UIButton* but=[UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"share_email.png"] forState:UIControlStateNormal];
    but.frame=CGRectMake(0,5, 210, 35);
    [shareViewController.view addSubview:but];
    
    but=[UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"share_sina.png"] forState:UIControlStateNormal];
    but.frame=CGRectMake(0,45, 210, 35);
    [shareViewController.view addSubview:but];
    
    but=[UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"share_tencent.png"] forState:UIControlStateNormal];
    but.frame=CGRectMake(0, 85, 210, 35);
    [shareViewController.view addSubview:but];
    
    if (pov==nil) {
        pov=[[UIPopoverController alloc] initWithContentViewController:shareViewController];
        pov.popoverContentSize=CGSizeMake(210, 130);
    }
    if (pov.popoverVisible) {
        [pov dismissPopoverAnimated:YES];
    }
    
    [pov presentPopoverFromRect:[topview viewWithTag:111].frame inView:topview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    
}

-(void)showList{
    if (list.alpha) {
        list.alpha=0;
    }else {
        list.alpha=1;
    }
    if (list==nil) {
        float keyboardH=(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)?352:262;
        CGFloat height=(viewWidth==1024)?748:1004;
        list=[[ListTable alloc] initWithFrame:CGRectMake(0, 60, keyboardH, height-keyboardH)];
        list.delegate=self;
        list.imId=mgid;
        list.dataArray=[NSArray arrayWithArray:doc.forums];
        [self.view addSubview:list];
        [list release];
    }
    [list.table reloadData];
}

- (void)pageSelect:(UIButton*)btn{
    [self selectThumb:btn];
    [self setBookPage:btn.tag];
    [self selectThumb:[thumbArray objectAtIndex:btn.tag]];
}

-(void)popView{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)markEvent{
    
    NSString* name=[UserCenter defaultUser].username;
    if (!name) {
        BOOL b;
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            b = YES;
        }
        else {
            b = NO;
        }
        LoginView*loginView=[[LoginView alloc] initWithH:b];
        if (b) {
            loginView.frame=CGRectMake(0, 55, 1024, HEIGHT_H);

        }else {
            loginView.frame=CGRectMake(0, 55, 768, HEIGHT_S);

        }
        [self.view addSubview:loginView];
        [loginView release];
        return;
    }
    if (mark.alpha) {
        mark.alpha=0;
        [mark.tv resignFirstResponder];
    }else {
        mark.alpha=1;
        [mark.tv becomeFirstResponder];
    }
    
    if (mark==nil) {
        float keyboardH=(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)?352:262;
        CGFloat height=(viewWidth==1024)?748:1004;
        mark=[[RemarkTable alloc] init];
        //        mark.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.7];
        [self.view addSubview:mark];
        [mark release];
        [mark reFreshFrame:CGRectMake(0, 60, viewWidth, height-keyboardH-60)];
    }
    if (mark.alpha==1) {
        if (mark.markArray) {
            [mark loadData];
        }
    }
}

-(void)bottomAnimate{    
    int h;
    if (viewWidth==1024) {
        h=748;
    }else {
        h=1004;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    CGPoint p=bottomView.center;
    if (bottomView.center.y>h) {
        bottomView.center=CGPointMake(p.x, p.y-165);
    }else {
        bottomView.center=CGPointMake(p.x, p.y+165);
        
    }
    [UIView commitAnimations];
}
-(void)selectThumb:(UIButton*)btn{
    for (UIButton* b in thumbArray) {
        if (b.tag==btn.tag) {
            b.selected=YES;
            b.layer.borderWidth=2;
            b.layer.borderColor=[[UIColor redColor] CGColor];
        }else {
            b.selected=NO;
            b.layer.borderWidth=0;
        }
    }
    
}
- (void)setBookPage:(NSInteger)index
{
    currentPage=index;
    [pageScroll setContentOffset:CGPointMake(currentPage*viewWidth, 0) animated:NO];
}
- (void)addCollection
{
    [collectionList removeAllObjects];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [collectionList addObjectsFromArray:[delegate getCollection]];
    
    BOOL isHave = NO;
    for (int i=0; i<[collectionList count]; i++) {
        NSDictionary *dic = [collectionList objectAtIndex:i];
        NSString *old_mgId = [dic objectForKey:@"mgId"];
        NSString *old_num = [dic objectForKey:@"num"];
        
        NSString *current_num = [NSString stringWithFormat:@"%d",currentPage];
        
        if ([old_mgId isEqualToString:[NSString stringWithFormat:@"%@",mgid]] && [old_num isEqualToString:current_num]) {
            isHave = YES;
            break;
        }
    }
    
    if (!isHave) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSString *time = [DataTools stringOfDate:[NSDate date] formatString:@"yyyy年MM月dd日 HH:mm"];
        [dic setObject:time forKey:@"time"];
        [dic setObject:[NSString stringWithFormat:@"%@",mgid] forKey:@"mgId"];
        [dic setObject:[NSString stringWithFormat:@"%d",currentPage] forKey:@"num"];
        [dic setObject:((Page *)[dataArray objectAtIndex:currentPage]).t_thumbnails forKey:@"t_thumbnails"];
        [collectionList addObject:dic];
        
        [delegate setCollectionList:collectionList];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReflashShoucangList" object: nil];
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"提示" 
                                                       message: @"成功添加到收藏" 
                                                      delegate: self 
                                             cancelButtonTitle: @"确定" 
                                             otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"提示" 
                                                       message: @"已存在于收藏列表中" 
                                                      delegate: self 
                                             cancelButtonTitle: @"确定" 
                                             otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}
#pragma mark listDelegate
-(void)listTableSelectedPage:(Page*)pag{
    [self setBookPage:pag.num.intValue];
    currentPage=pag.num.intValue;
}
#pragma mark scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (ratating) {
        return;
    }
    currentPage= (int)scrollView.contentOffset.x/scrollView.frame.size.width;
    NSLog(@" didend===%d",currentPage);
    [self performSelector:@selector(selectThumb:) withObject:[thumbArray objectAtIndex:currentPage] afterDelay:0.3];
}

#pragma mark parser
-(void)parseXML{
    NSString* xmlPath= [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/index.xml",mgid]];
    NSXMLParser* parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:xmlPath]];
    parser.delegate=self;
    if ([parser parse]) {
    }
    [parser release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    NSLog(@"elementName------%@",elementName);
    if ([elementName isEqualToString:@"Document"]) {
        doc=[[Document alloc] init];
        doc.total=[attributeDict valueForKey:@"total"];
    }
    if ([elementName isEqualToString:@"Forum"]) {
        forum=[[Forum alloc] init];
        forum.title=[attributeDict valueForKey:@"title"];
        forum.index=[attributeDict valueForKey:@"index"];
    }
    if ([elementName isEqualToString:@"Article"]) {
        article=[[Article alloc] init];
        article.title=[attributeDict valueForKey:@"title"];
        article.num=[attributeDict valueForKey:@"num"];
    }
    if ([elementName isEqualToString:@"Page"]) {
        page=[[Page alloc] init];
        page.t_enlarge=[attributeDict valueForKey:@"t-enlarge"];
//        NSLog(@"page.t_enlarge------%@",page.t_enlarge);
        
        page.num=[attributeDict valueForKey:@"num"];
        page.t_htmlref=[attributeDict valueForKey:@"t-htmlref"];
//        NSLog(@"t_htmlref------%@",page.t_htmlref);
        
        page.t_thumbnails=[attributeDict valueForKey:@"t-thumbnails"];
//        NSLog(@"t_thumbnails------%@",page.t_thumbnails);
        
        page.v_enlarge=[attributeDict valueForKey:@"h-enlarge"];
//        NSLog(@"page.v_enlarge------%@",page.v_enlarge);
        
        page.v_htmlref=[attributeDict valueForKey:@"h-htmlref"];
//        NSLog(@"page.v_htmlref------%@",page.v_htmlref);
        
        page.v_thumbnails=[attributeDict valueForKey:@"h-thumbnails"];
//        NSLog(@"page.v_thumbnails------%@",page.v_thumbnails);
        
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"parse end===%@,%@,%@",elementName,namespaceURI,qName);
    if ([elementName isEqualToString:@"Page"]) {
        [article.pages addObject:page];
        [page release];
        page=nil;
//        NSLog(@"%@",article.pages);
    }
    if ([elementName isEqualToString:@"Article"]) {
        [forum.articles addObject:article];
        [article release];
        article=nil;
    }
    if ([elementName isEqualToString:@"Forum"]) {
        [doc.forums addObject:forum];
//        NSLog(@"%@",forum);
        [forum release];
        forum=nil;
        
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    currentPage=0;
    [self viewWidth:self.interfaceOrientation];
    [self parseXML];
    [self initTopView];
    [self initBottomView];
    [self initScroll];
    [self addPageToScroll:self.interfaceOrientation];
    [self selectThumb:[thumbArray objectAtIndex:currentPage]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    ratating=NO;
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [self viewWidth:toInterfaceOrientation];
    
//    float keyboardH=(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)?352:262;
    CGFloat height=(viewWidth==1024)?748:1004;

    ratating=YES;
    LoginView*loginView=(LoginView*)[self.view viewWithTag:9090];
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight||toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        loginView.frame=CGRectMake(0, 55, 1024, HEIGHT_H);
    }else {
        loginView.frame=CGRectMake(0, 55, 768, HEIGHT_S);
    }
    [pageScroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    pageScroll.frame=CGRectMake(0, 0, viewWidth, height);
    pageScroll.contentSize=CGSizeMake(viewWidth*dataArray.count, height);

    [self addPageToScroll:toInterfaceOrientation];
    for (UIView*b in pageScroll.subviews) {
        b.alpha=0;
    }
    [self setBookPage:currentPage];

    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:duration];
    [self refreshSubViewsDidRotate];
    for (UIView* sub in pageScroll.subviews) {
        sub.alpha=1;
    }
    [UIView commitAnimations];
}

-(void)refreshSubViewsDidRotate{
    float keyboardH=(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)?352:262;
    CGFloat height=(viewWidth==1024)?748:1004;
    [mark reFreshFrame:CGRectMake(0, 60, viewWidth, height-keyboardH-60)];
    contents.frame=CGRectMake(0, 35, viewWidth, 165);
    
    topview.frame=CGRectMake(0, 0, viewWidth, 55);
    UIButton* b=(UIButton*)[topview viewWithTag:111];
    b.frame=CGRectMake(viewWidth-80, 0, 40, 55);
    thumbBtn.frame=CGRectMake(10, 0, 35, 35);
    UIButton*markbtn=(UIButton*)[topview viewWithTag:222];
    markbtn.frame=CGRectMake(viewWidth-140, 0, 40, 55);
    
    UIButton* add=(UIButton*)[topview viewWithTag:333];
    add.frame=CGRectMake(viewWidth-200, 0, 40, 55);
    bottomView.frame=CGRectMake(0, height-35,  viewWidth, bottomViewheight);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
@synthesize mgid;
@end
